var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "定位中...",
    copyBanners:{},
    indexCopylists:{},
    indexListStores:{},
    latitude: '',
    longitude: '',
    loading:true,
    loadingComplete:true,
    page:0,  //当前请求第几页
    countPage:'',
    pageSize:3,  //每页数据量
    hasMore: true,
    storeList: [],
  },
  //indexCopy页更多
  clickMore: function () {
    wx.navigateTo({
      url: '../more/more'
    })
  },
  //indexCopy页轮播
  indexCopyBanner: function () {
    let that = this
    util.request(api.IndexLocalBanner, {
      wei: wx.getStorageSync('wei'),
      jing: wx.getStorageSync('jing')
    }, 'POST').then(function (res) {

      if (res.data.code == 200) {
        that.setData({
          copyBanners: res.data.data.banner,
        })
      }

    })
  },
  //商家模板页分类
  indexCopylists: function () {
    let that = this
    util.request(api.IndexListUrl).then(function (res) {

      if(res.data.code == 200){
        that.setData({
          indexCopylists: res.data.data
        })
      }
      
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var address = wx.getStorageSync('add')
    this.setData({
      address:address,
      sp_tyid:parseInt(options.sp_tyid)
    })
    this.indexListStore(0)
    this.indexCopyBanner()
    this.indexCopylists()
    this.getCountShops()
  },

  //获取该分类该位置商户总条数
  getCountShops:function(){
    var that = this;
    util.request(api.GetCountShops,{
      sp_tyid: that.data.sp_tyid,
      jing: wx.getStorageSync('jing'),
      wei: wx.getStorageSync('wei'),
    }).then(function(res){
      if (res.data.code == 200) {
        that.data.countPage = Math.ceil(res.data.data.num / that.data.pageSize);
      }
    })
  },
  //页面下拉刷新
  onPullDownRefresh: function () {
    let that = this
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      success: function () {
        that.indexListStore(0)
        that.setData({
          loading: true,
          loadingComplete: true
        })
      }
    })
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    var that = this
    if(this.data.page > (this.data.countPage-2)){
      this.setData({
         loading: true,
         loadingComplete: false
      })
    }else{
      this.setData({
        loading: false,
      })
      setTimeout(function () {
        that.data.page++;
        that.indexListStore(that.data.page);
      }, 800)
    }
  },
  //加载店铺列表
  indexListStore:function(p){
    var that = this;
    var jing = wx.getStorageSync('jing')
    var wei = wx.getStorageSync('wei')
    var page = p
    util.request(api.IndexNextStore,{
      sp_tyid: that.data.sp_tyid,
      jing: jing,
      wei: wei,
      page: page,
      num: that.data.pageSize
    }).then(function(res){
      if (res.data.code == 200) {
        if (page == 0) {
         
          that.setData({
            storeList: res.data.data.shops,
            copyStoreName: res.data.data.ty_name
          })
        } else {
          that.setData({
            storeList: that.data.storeList.concat(res.data.data.shops),
          })
        }
      }
    })

  },
})
